package services

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/golang/protobuf/ptypes/wrappers"

	"kop/framework/errors"
	"kop/modules/item/consts"
	"kop/pb"

	captainC "kop/modules/captain/client"
	itemC "kop/modules/item/client"
)

func (s *CaptainService) MyTrainSeats(ctx context.Context, _ *empty.Empty) (*pb.TrainSeats, error) {

	var roleID = s.GetRoleID(ctx)

	return captainC.GetTrainSeats(roleID), nil
}

func (s *CaptainService) TrainUnlock(ctx context.Context, _ *empty.Empty) (*pb.TrainUnlockReply, error) {

	var reply pb.TrainUnlockReply

	var roleID = s.GetRoleID(ctx)

	var seats = captainC.GetTrainSeats(roleID)

	if seats.UnlockDiamond < 1 {
		return &reply, errors.ParamError
	}

	var err error
	if reply.Item, err = itemC.DelItem(roleID, s.GetServerID(ctx), consts.Diamond, seats.UnlockDiamond); err != nil {
		return &reply, err
	}

	var seat int32 = 0
	for _, v := range seats.TrainSeats {
		if v.Seat > seat {
			seat = v.Seat
		}
	}

	seat += 1
	reply.TrainSeat = captainC.TrainUnlock(roleID, seat)
	return &reply, nil
}

// 开始训练
func (s *CaptainService) Train(ctx context.Context, req *pb.APITrainRequest) (*pb.TrainSeat, error) {

	return captainC.CaptainService.Train(ctx, req)
}

// 完成训练
func (s *CaptainService) TrainFinish(ctx context.Context, req *wrappers.Int32Value) (*pb.APITrainFinishReply, error) {

	return captainC.CaptainService.TrainFinish(ctx, req)
}
